﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TDTool.Utils;

namespace TDTool.Collections
{
    /// <summary>
    /// TdTableWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TdTableWindow : Window
    {
        const int ANIM_TIME = 1;

        bool IsActivated = false;
        public TdTableWindow()
        {

            InitializeComponent();

            Init();
        }

        private void Init()
        {
            this.KeyDown += TdTableWindow_KeyDown;
        }
        #region 各种事件
        private void TdTableWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Console.WriteLine("You press the 'enter' .");
            }
            else if (e.Key == Key.LeftCtrl)
            {
                Console.WriteLine("You press the 'LeftCtrl' .");
            }
        }

        /// <summary>
        /// 窗口拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Closed(object sender, EventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitNoAltTabShow();
            FloatWindow_Loaded();
        }

        // 窗体在加载后，控制它显示在屏幕最前端，以及位置。
        void FloatWindow_Loaded()
        {
            this.Left = SystemParameters.PrimaryScreenWidth / 2 - 150;
            this.Top = 0;
        }

        private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }
            SaveTD();
        }
        private void ClearMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ClearAllInput();
        }
        private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
        {
            string path = Util.GetTDRepertoryPath();
            bool isOpenSuccess = Util.OpenFolder(path);
            if (!isOpenSuccess)
            {
                MessageBox.Show("路径不存在：" + path);
            }
        }
        private void CloseMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_MouseEnter(object sender, MouseEventArgs e)
        {
            Console.WriteLine("进入");
            AnimOpacityShow();
        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            Console.WriteLine("===>>> 离开");
            AnimOpacityHidden();
        }
        private void Window_Activated(object sender, EventArgs e)
        {
            Console.WriteLine("活跃");
            IsActivated = true;
        }
        private void Window_Deactivated(object sender, EventArgs e)
        {
            Console.WriteLine("--------不活跃");
            IsActivated = false;
            AnimOpacityHidden();
        }
        #endregion
        // Alt+Tab时 不显示该Window
        private void InitNoAltTabShow()
        {
            WindowInteropHelper wndHelper = new WindowInteropHelper(this);
            WindowHelper.Window_Loaded(wndHelper);
        }
        private void AnimOpacityShow()
        {
            if (RootWindow.Opacity == 1.00)
                return;
            DoubleAnimation opacityAnim = new DoubleAnimation();
            opacityAnim.To = 1.00;
            opacityAnim.Duration = TimeSpan.FromSeconds(ANIM_TIME);
            RootWindow.BeginAnimation(Window.OpacityProperty, opacityAnim);
        }
        private void AnimOpacityHidden()
        {
            if (IsActivated || RootWindow.Opacity == 0.01)
                return;
            DoubleAnimation opacityAnim = new DoubleAnimation();
            opacityAnim.To = 0.01;
            opacityAnim.Duration = TimeSpan.FromSeconds(ANIM_TIME);
            RootWindow.BeginAnimation(Window.OpacityProperty, opacityAnim);
        }
        private void ClearAllInput()
        {
            TdNumberTb.Clear();
            TdNameTb.Clear();
            QuestionReasonTb.Clear();
            ReCorrectWayTb.Clear();
        }

        #region Check the input
        private bool CheckInput()
        {
            //bool isInputRight = true;
            if (!IsTDNumberRight(TdNumberTb.Text.Trim()))
                return false;
            if (!IsTDNameRight(TdNameTb.Text.Trim()))
                return false;

            return true;
        }

        private bool IsTDNumberRight(string tdNumber)
        {
            if (string.IsNullOrEmpty(tdNumber))
            {
                MessageBox.Show("TD单号，不能为空");
                return false;
            }
            if (!Util.IsInt(tdNumber))
            {
                MessageBox.Show("TD单号，请输入数字");
                return false;
            }
            return true;
        }
        private bool IsTDNameRight(string tdName)
        {
            if (string.IsNullOrEmpty(tdName))
            {
                MessageBox.Show("TD名，不能为空");
                return false;
            }
            return true;
        }

        #endregion

        private void SaveTD()
        {
            // 1. 生成文件名
            string newTDFileName = Util.GetNewTDFileName(TdNumberTb.Text.Trim(), TdNameTb.Text.Trim());
            string newTDFilePath = Util.GetTDRepertoryPath() + newTDFileName;
            // 2. 检测是否存在
            if (Util.IsFileExist(newTDFilePath))
            {
                MessageBox.Show("该文件已存在"+newTDFilePath+"，请检查");
                return;
            }
            // 2.2 不存在：创建文件，写入数据
            Util.CreateNewTDFile(newTDFilePath, BuildTDFileContent());
        }
        /// <summary>
        /// 将输入整理，形成txt中的内容
        /// </summary>
        /// <returns></returns>
        private string BuildTDFileContent()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\r\nTD单号：");
            sb.Append(TdNumberTb.Text.Trim());

            sb.Append("\r\n\r\nTD名称：");
            sb.Append(TdNameTb.Text.Trim());

            sb.Append("\r\n\r\n===============================================");
            sb.Append("\r\n问题原因|\r\n");
            sb.Append("---------\r\n");
            sb.Append(QuestionReasonTb.Text.Trim());

            sb.Append("\r\n\r\n\r\n===============================================");
            sb.Append("\r\n修改方法|\r\n");
            sb.Append("---------\r\n");
            sb.Append(ReCorrectWayTb.Text.Trim());
            return sb.ToString();
        }
    }
}
